#include "playRoleBook.h"
#include "jsonUtils.h"
#include "fileUtils.h"

PlayRoleBook::~PlayRoleBook()
{
	if (!images.empty()) {
		for (std::list<ImageFile*>::iterator it = images.begin(); it != images.end();++it) {
			ImageFile*& p = *it;
			if (p) {
				delete p;
				p = NULL;
			}
		}
		images.clear();
	}
}

void PlayRoleBook::fromJson(const JsonValue& value)
{
	JsonUtils::loadLong(value, L"id", id);
	JsonUtils::loadLong(value, L"playRoleId", playRoleId);
	JsonUtils::loadLong(value, L"chapterId", chapterId);
	JsonUtils::loadStr(value, L"name", name);
	JsonUtils::loadInt(value, L"sequence", sequence);
	JsonUtils::loadStr(value, L"filePath", filePath);
	loadImageArr(value, images);
}

void PlayRoleBook::toJson(JsonValue& value, JsonDoc::AllocatorType& allocator, bool toConfig) const
{
	JsonUtils::addString(value, L"name", name, allocator);
	if (toConfig) {
		value.AddMember(L"id", id, allocator);
		value.AddMember(L"playRoleId", playRoleId, allocator);
		value.AddMember(L"chapterId", chapterId, allocator);
		value.AddMember(L"sequenc", sequence, allocator);
		JsonUtils::addString(value, L"filePath", filePath, allocator);
	}
	else {
		JsonUtils::addString(value, L"html", FileUtils::url(movieId, ROLE_BOOK, id), allocator);
	}
	if (!images.empty()) {
		JsonValue arr(rapidjson::kArrayType);
		for (std::list<ImageFile*>::const_iterator it = images.begin(); it != images.end(); ++it) {
			ImageFile* const& p = *it;
			JsonValue item(rapidjson::kObjectType);
			p->toJson(item, allocator, toConfig);
			arr.PushBack(item, allocator);
		}
		value.AddMember(L"images", arr, allocator);
	}
}

const int64_t& PlayRoleBook::getId() const
{
	return id;
}

void PlayRoleBook::setMovieId(const int64_t& movieId)
{
	this->movieId = movieId;
}

const String& PlayRoleBook::getFilePath() const
{
	return filePath;
}

int64_t PlayRoleBook::getRoleId() const
{
	return playRoleId;
}

const std::list<ImageFile*>& PlayRoleBook::getImages()
{
	return images;
}

void PlayRoleBook::loadImageArr(const rapidjson::GenericArray<true, JsonValue>& arr)
{
	loadImageArr(arr, images);
}

const ImageFile* PlayRoleBook::findImageFile(const String& lastPath)
{
	if (!images.empty()) {
		for (std::list<ImageFile*>::iterator it = images.begin(); it != images.end();++it) {
			ImageFile*& p = *it;
			if (p && p->pathEndWith(lastPath)) {
				return p;
			}
		}
	}
	return nullptr;
}

const ImageFile* PlayRoleBook::findImageFile(const int64_t& id)
{
	if (!images.empty()) {
		for (std::list<ImageFile*>::iterator it = images.begin(); it != images.end();++it) {
			ImageFile*& p = *it;
			if (p && p->getId() == id) {
				return p;
			}
		}
	}
	return nullptr;
}

void PlayRoleBook::loadImageArr(const rapidjson::GenericArray<true, JsonValue>& arr, std::list<ImageFile*>& list)
{
	for (JsonValue::ConstValueIterator v = arr.Begin();v != arr.End();++v) {
		if (v->IsObject()) {
			ImageFile* image = new ImageFile();
			image->setMovieId(movieId);
			image->fromJson(*v);
			image->relative2html(filePath);
			list.push_back(image);
		}
	}
}

void PlayRoleBook::loadImageArr(const JsonValue& v, std::list<ImageFile*>& list)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(L"images");
	if (it != v.MemberEnd() && it->value.IsArray()) {
		rapidjson::GenericArray<true, JsonValue> arr = it->value.GetArray();
		loadImageArr(arr, list);
	}
}
